package com.gimi.cloud.mmf.user.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gimi.cloud.mmf.common.utils.JsonUtil;
import com.gimi.cloud.mmf.user.common.enums.ResponseCodeEnum;
import com.gimi.cloud.mmf.user.common.reqhead.Response;
import com.gimi.cloud.mmf.user.common.reqhead.Util;
import com.gimi.cloud.mmf.user.entity.TestCapitalLog;
import com.gimi.cloud.mmf.user.entity.TestUser;
import com.gimi.cloud.mmf.user.service.TestUserService;

@RestController
@RequestMapping("user")
public class TestUserController {
	
	@Autowired private TestUserService testUserService;
	
	//级联保存需要实体添加cascade属性
	@RequestMapping("save")
	public void saveUser(@RequestBody TestUser user, HttpServletResponse response) {
		List<TestCapitalLog> list = new ArrayList<>();
		TestCapitalLog testCapitalLog = new TestCapitalLog();
		testCapitalLog.setMoney(BigDecimal.valueOf(888888));
		testCapitalLog.setPurpose("发工资了");
		testCapitalLog.setTestUser(user);
		list.add(testCapitalLog);
		testCapitalLog = new TestCapitalLog();
		testCapitalLog.setMoney(BigDecimal.ZERO.subtract(BigDecimal.valueOf(888888)));
		testCapitalLog.setPurpose("用光了");
		testCapitalLog.setTestUser(user);
		list.add(testCapitalLog);
		user.setTestCapitalLogs(list);
		testUserService.save(user);
		Response responseStr = new Response();
		responseStr.setResponseCode(ResponseCodeEnum.SUCCESSED);
		Util.sendResponse(JsonUtil.objectToJson(responseStr), response);
	}
	
	//级联保存需要实体添加cascade属性
	@RequestMapping("query")
	public void queryUser(@RequestParam("userName")String userName, Integer page, Integer size) {
		PageRequest pageable = new PageRequest(page, size);
//		testUserService.queryByHql(userName, pageable);
//		testUserService.queryMoreTableByHql(userName, pageable);
//		testUserService.queryBySql(userName, pageable);
		testUserService.queryMoreTableBySql(userName, pageable);
//		testUserService.queryByMethodName(userName, pageable);
	}
	
	
	
	
	
	
}
